/**
*******************************************************************
Copyright (C) 2011  Elizar D. Pepino

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
 
*********************************************************************
* @description:	Basic pagination plugin for jquery
* @author: Elizar Pepino
* @version: 0.0.1
* @site: http://d2980014.u81.secureserverdot.com/ ( my temporary site domain name already expired )
* 
* @parameters
*
*	(string)		prevBtn						- an element that will be used as a previous button
*	(string)		nextBtn						- an element that will be used as a next button
*	(string)		currentPageTextLabel		- A text label that will display the value of the current page
*	(string)		totalPageTextLabel			- A text label that will display the number of total pages
*	(string)		currentPageTextInput		- a text input that will display and set the value of the current page
*	(string)		effect						- either "fade" or "slide"
*	(string)		easing						- an easing that will be used for the animation defaults to "easeInOutExpo"
*	(string)		filters						- child elements of the target that will not be included
*	(function)		callback					- a function that will be called when the current page has done animating
*
* @Log:
*			
**/

jQuery.fn.extend({
	basicpage: function(options) {
		return this.each(function() {
			new jQuery.BasicPage(this, options);
		});
	}
});

// numeric only for input field
jQuery.fn.forceNumeric =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow enter, backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 || 
                key == 9 ||
				key == 13 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};


jQuery.BasicPage = function(target, options) {
	
	// Setup option var
	var _options = options || {};
	_options.prevBtn = _options.prevBtn || ".basicpagePrevBtn";
	_options.nextBtn = _options.nextBtn || ".basicpageNextBtn";
	_options.currentPageTextLabel = _options.currentPageTextLabel || ".currentPageTextLabel";
	_options.totalPageTextLabel = _options.totalPageTextLabel || ".totalPageTextLabel";
	_options.currentPageTextInput = _options.currentPageTextInput || ".currentPageTextInput";
	_options.effect = _options.effect || "fade";
	_options.easing = _options.easing || "easeInOutExpo";
	_options.filters = _options.filters || "a";
	_options.callback = _options.callback || function(){};
	
	// Setup initials
	var firstRun;
	var _target = $(target);
	var _currentPage = 1;
	var _numOfPages = _target.children().length;
	
	// set current page text input to numeric only
	$(_options.currentPageTextInput).forceNumeric();
	
	// do first run
	doPaginate(_currentPage);
	
	// bind enter event to the current page text input
	$(_options.currentPageTextInput).keypress(function(e) {
	        if(e.which == 13) {
	            var t = $(this);
				t.blur();
	           	var p = t.val() > 0 && t.val() < _numOfPages ? t.val() : _currentPage;
				if(p != _currentPage)
				{
					doPaginate(p);
				}
				else
				{
					t.val(_currentPage);
					t.focus();
				}
	        }
	});
	
	// buttons
	$(_options.prevBtn).click(function(e){
		if (_currentPage > 1) {
			doPaginate(_currentPage - 1);
		}
		return false; 
	});
	$(_options.nextBtn).click(function(e){
		if (_currentPage < _numOfPages) {
			doPaginate(_currentPage + 1);
		}
		return false;
	});
	
	// do pagination
	function doPaginate(p)
	{
		
		var first;
		if(!firstRun){
			firstRun = 1;
			first = true;
		}
		else
		{
			first = false;
		}
		
		var index = p-1;
		// check to see if this target is visible
			
			// switch to the next content
		if (_target.is(":visible") || _target.css("visibility") == "hidden") {
			if($(_target).children("div, ul").not($(_options.filters)).length >= 1)
			{
				$(_target).children("div, ul").not($(_options.filters)).each(function(cIndex){
					
					// then show the selected tab
					if(cIndex == index)
					{
						if(_options.effect == "fade")
						$(this).fadeIn({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
						else if(_options.effect == "slide")
						$(this).slideDown({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
						else
						$(this).fadeIn({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
							
						// set new current page
						_currentPage = index + 1;
						// changed text labels value
						$(_options.currentPageTextInput).blur();
						$(_options.currentPageTextInput).val(_currentPage);
						$(_options.currentPageTextLabel).text(_currentPage);
						$(_options.totalPageTextLabel).text(_numOfPages);
					}
					else
					{
						// hide all tabs first
						$(this).hide();
					}
				});
			}
		}
			
		
	}
	
}